#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QFile>
#include <iostream>
#include <QMessageBox>
#include <QProcess>
#include <windows.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    std::cout<<"niubi"<<std::endl;
}

bool MainWindow::copyFile(const QString &fromFIleName, const QString &toFileName)
{
    char* byteTemp = new char[4096];//字节数组
    int fileSize = 0;
    int totalCopySize = 0;
    QFile tofile;
    ui->progressBar->setValue(0);
    tofile.setFileName(toFileName);
    tofile.open(QIODevice::WriteOnly);
    QDataStream out(&tofile);
    out.setVersion(QDataStream::Qt_4_8);

    QFile fromfile;
    fromfile.setFileName(fromFIleName);
    if(!fromfile.open(QIODevice::ReadOnly)){
        std::cout << "open fromfile failed！！！"<<std::endl;
        return false;
    }
    fileSize = fromfile.size();
    QDataStream in(&fromfile);

    in.setVersion(QDataStream::Qt_4_8);
    ui->progressBar->setRange(0, fileSize);
    while(!in.atEnd())
    {
        int readSize = 0;
        //读入字节数组,返回读取的字节数量，如果小于4096，则到了文件尾
        readSize = in.readRawData(byteTemp, 4096);
        out.writeRawData(byteTemp, readSize);
        totalCopySize += readSize;
        ui->progressBar->setValue(totalCopySize);
    }
    if(totalCopySize == fileSize){
        tofile.setPermissions(QFile::ExeUser);
        return true;
    }
    else
        return false;
}

void MainWindow::setUrl(QString newUrl)
{
    downloadUrl = newUrl;
    ui->lineEdit_url->setText(downloadUrl);
}

void MainWindow::setFileName(QString packName)
{
    downloadFileName = packName;
    ui->lineEdit_fileName->setText(packName);

}

void MainWindow::on_pushButton_2_clicked()
{
    ui->label_state->setText("downloading");
    downloadUpdatePack();
    ui->label_state->setText("download success");
    auto btn = QMessageBox::information(nullptr,"提示","是否安装",QMessageBox::Yes|QMessageBox::No);
    if(btn == QMessageBox::Yes){
        std::cout<<"guanle"<<std::endl;
        Sleep(1000);

        auto process = new QProcess;
        process->start("C:/git/AutoUpdate/build-AutoUpdate-Desktop_Qt_5_14_2_MinGW_64_bit-Debug/Master/debug/Master.exe");
        QApplication::quit();
    }else{

    }
}

void MainWindow::downloadUpdatePack()
{
    if(0){//本地文件测试
        auto state = QFile::remove("C:/git/AutoUpdate/build-AutoUpdate-Desktop_Qt_5_14_2_MinGW_64_bit-Debug/Master/debug/Master.exe");
        copyFile("C:/git/AutoUpdate/build-AutoUpdate-Desktop_Qt_5_14_2_MinGW_64_bit-Debug/Master/debug/Master_new.exe",
                 "C:/git/AutoUpdate/build-AutoUpdate-Desktop_Qt_5_14_2_MinGW_64_bit-Debug/Master/debug/Master.exe");
    }else{

        if(reply){
            disconnect(reply,&QNetworkReply::finished,this,&MainWindow::on_finished);
            disconnect(reply,&QNetworkReply::readyRead,this,&MainWindow::on_readyRead);
            disconnect(reply,&QNetworkReply::downloadProgress,this,&MainWindow::on_downloadProgress);
            on_finished();
        }
        if(downFile){
            downFile->close();
            delete downFile;
            downFile = nullptr;
        }
        downFile = new QFile("./"+downloadFileName);

        if(!downFile->open(QIODevice::WriteOnly)){
            std::cout<<"local file open fail"<<std::endl;
        }else
            std::cout<<"local file open success"<<std::endl;
        reply = networkManager->get(QNetworkRequest(downloadUrl));
        connect(reply,&QNetworkReply::finished,this,&MainWindow::on_finished);
        connect(reply,&QNetworkReply::readyRead,this,&MainWindow::on_readyRead);
        connect(reply,&QNetworkReply::downloadProgress,this,&MainWindow::on_downloadProgress);
    }
}

void MainWindow::init()
{
    networkManager = new QNetworkAccessManager;
}

void MainWindow::on_finished()
{
    downFile->close();
    delete downFile;
    downFile= nullptr;
    reply->deleteLater();
    reply = nullptr;
}

void MainWindow::on_readyRead()
{
    downFile->write(reply->readAll());
}

void MainWindow::on_downloadProgress(qint64 bytesRead, qint64 totalBytes)
{
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(bytesRead);
}
